using System.Collections;
using System.Collections.Generic;
using System.Linq;
using QFramework;
using UnityEngine;

public class CardPackStoreManager : MonoBehaviour, IController
{
    [SerializeField] GameObject cardPerfab;
    [SerializeField] Transform cardPoolTransform;

    public CardData CardDataInfos { get; set; }
    public List<GameObject> Cards { get; set; } = new List<GameObject>();

    public IArchitecture GetArchitecture()
    {
        return CardGame.Interface;
    }

    void Awake()
    {
        this.CardDataInfos = this.GetModel<CardData>();
    }

    public void OnClickDrawCard()
    {
        foreach (GameObject card in this.Cards)
        {
            Destroy(card);
        }

        this.Cards.Clear();

        for (int i = 0; i < 5; i++)
        {
            var card = this.CardDataInfos.Cards[Random.Range(0, this.CardDataInfos.Cards.Count)];
            var cardGameObject = Instantiate(this.cardPerfab, this.cardPoolTransform);
            cardGameObject.GetComponent<CardDisplay>().Card = card;
            this.Cards.Add(cardGameObject);

            var id = card.Id;

            if (this.CardDataInfos.ObtainedCards.ContainsKey(id))
            {
                this.CardDataInfos.ObtainedCards[id] += 1; // 卡数量+1
            }
            else
            {
                this.CardDataInfos.ObtainedCards.Add(id, 1);
            }
        }
        
        this.CardDataInfos.SaveObtainedCards();
    }
}
